package com.zx.sercurity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.ProxyInfo;
import android.text.TextUtils;
import android.util.Log;

/**
 * 网络代理风险
 */
public class NetProxyRisk implements RiskControlInterface<RiskResult.NetProxyRiskResult> {

    @Override
    public RiskResult.NetProxyRiskResult riskCheck(Context context) {
        return isProxyEnabled(context) ? new RiskResult.NetProxyRiskResult(true) : new RiskResult.NetProxyRiskResult(false);
    }

    public boolean isProxyEnabled(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                ProxyInfo defaultProxy = connectivityManager.getDefaultProxy();
                return defaultProxy != null;
            }
        }
        return false;
    }

    /**
     * @return 是否开启了代理
     */
    public boolean checkWifiProxy() {
        String proxyAddress = System.getProperty("http.proxyHost");
        int proxyPort = 0;
        String portStr = System.getProperty("http.proxyPort");
        if (!TextUtils.isEmpty(portStr)) {
            proxyPort = Integer.parseInt(portStr);
        }
        Log.i("ProxyUtil", "地址:" + proxyAddress + " 端口：" + proxyPort);
        return !TextUtils.isEmpty(proxyAddress) && proxyPort != 0;
    }
}
